"""
Pydantic 模型用于数据验证和清洗。
"""

from pydantic import BaseModel, validator
from pydantic.v1 import Field
from typing import Optional, Literal


class BaseData(BaseModel):
    device_id: str = Field(..., min_length=15, max_length=15, description="设备唯一标识")
    action: Literal["location", "health", "battery", "warning", "audio"]
    timestamp: int = Field(..., gt=0)

    class Config:
        extra = "allow"


class LocationData(BaseData):
    """
    定位数据模型。
    """
    longitude: float = Field(..., description="经度（高德坐标）")
    latitude: float = Field(..., description="纬度（高德坐标）")


class WarningData(BaseData):
    """
    预警数据模型。
    """
    type: int = Field(..., description="预警类型（1: 心率超标, 2: SOS 预警, 3: 电量过低）")
    longitude: Optional[str] = Field(None, description="经度（高德坐标）")
    latitude: Optional[str] = Field(None, description="纬度（高德坐标）")


class Audio(BaseModel):
    """
    预警数据模型。
    """
    voice: str = Field(..., description="音频文件BASE64编码")

    class Config:
        extra = "allow"


class AudioData(BaseData):
    """
    预警数据模型。
    """
    audio: Audio = Field(..., description="音频文件BASE64编码")

    class Config:
        extra = "allow"


class HealthData(BaseData):
    """
    健康数据模型。
    """
    heart_rate: int = Field(0, description="心率")
    blood_oxygen: int = Field(0, description="血氧")
    body_temperature: float = Field(None, description="体温")
    blood_pressure_high: Optional[int] = Field(None, description="收缩压")
    blood_pressure_low: Optional[int] = Field(None, description="舒张压")


class BatteryData(BaseData):
    """
    电量及计步数据模型。
    """
    battery: int = Field(..., description="电量值")
    pedometer: int = Field(..., description="计步数")
